package com.itggm.controller;

import com.itggm.DTO.CoachPageDTO;
import com.itggm.pojo.Coach;
import com.itggm.response.ResponseResult;
import com.itggm.service.CoachService;
import com.itggm.utils.AliOSSUtil;
import com.itggm.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author: ggm
 * @data 2024年11月20日 20:30
 */
@RestController
@RequestMapping("coach")
public class CoachController {
    @Autowired
    private CoachService coachService;
    @Autowired
    private AliOSSUtil aliOSSUtil;

    /**
     * @Author: ggm
     * @date: 2024/11/20
     * 分页查询
     */
    @GetMapping("/page")
    public ResponseResult page(CoachPageDTO coachPageDTO) {
        return coachService.page(coachPageDTO);
    }

    /**
     * @Author: ggm
     * @date: 2024/11/20
     * 文件上传
     */
    @PostMapping("upload")
    public ResponseResult upload(MultipartFile file) throws IOException {
        String url = aliOSSUtil.upload(file);
        return ResponseResult.success().data(url);
    }

    /**
     * @Author: ggm
     * @date: 2024/11/20
     * 添加教练
     */
    @PostMapping("/uploadInfo")
    public ResponseResult add( Coach coach) {
        return coachService.uploadInfo(coach);


    }
    /**
     * @Author: ggm
     * @date:  2024/11/21
     * 前端获取教练信息
     */
    @GetMapping("/front/list")
    public ResponseResult getCoachInfo(Integer limit){
        return coachService.getCoachInfo(limit);
    }
     /**
      * @Author: ggm
      * @date:  2024/11/21
      * 删除教练
      */
     @PostMapping("/delete")
     public ResponseResult delete(String url){
         String fileName = StringUtil.extractFileNameFromUrl(url);
        try {
            //从阿里云删除文件
            aliOSSUtil.delete(fileName);
            //从数据库删除文件
            coachService.delete(url);
            return ResponseResult.success().message("文件删除成功");
        } catch (Exception e) {
            return ResponseResult.fail().message("文件删除失败");
        }
     }
      /**
       * @Author: ggm
       * @date:  2024/11/22
       * 更新教练信息
       */
      @PostMapping("/update")
      public ResponseResult updateCoachInfo(Coach coach){
          return coachService.updateCoachInfo(coach);

      }

    /**
     * @Author: ggm
     * @date:  2024/11/25
     * 获取教练列表
     */
    @GetMapping("/list")
    public ResponseResult getCoachList(){
        return coachService.getCoachList();
    }
     /**
      * @Author: ggm
      * @date:  2024/11/26
      * 修改图片
      */
     @PostMapping("/uploadImg")
     public ResponseResult updateImg(String coachId, String fileName, String url){
         return coachService.updateImg(coachId,fileName,url);
     }







}
